package com.htc.controller;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.htc.model.Consumer;
import com.htc.model.InternalResponse;
import com.htc.model.ProductComment;
import com.htc.model.request.ProductCommentRequest;
import com.htc.service.ProductCommentService;

@RestController
@RequestMapping("/productComment")
public class ProductCommentController {

	@Autowired
	private ProductCommentService productCommentService;
	
	@RequestMapping("/cud")
	public InternalResponse cud(ProductComment t,HttpSession session,@Param("type")String type) {
		InternalResponse internalResponse = null;
		
		try {
			switch (type) {
			case "c":
				Consumer consumer = (Consumer) session.getAttribute("consumerkey");
				t.setUser_id(consumer.getId());
				productCommentService.add(t);
				break;
			case "u":
				productCommentService.update(t);			
				break;
			case "d":
				productCommentService.del(t.getId());
				break;
			default:
				break;
			}
			
			internalResponse=InternalResponse.success();
		} catch (Exception e) {
			internalResponse=InternalResponse.fail();
		}
		return internalResponse;
	}
	
	@RequestMapping("/r")
	public InternalResponse r(ProductCommentRequest request) {
		InternalResponse internalResponse = null;
		try {
			
			internalResponse=InternalResponse.success().withBody(productCommentService.queryList(request)) ;
		} catch (Exception e) {
			internalResponse=InternalResponse.fail();
		}
		return internalResponse;
	}
	
}
